package cn.iutils.faq.controller;

import cn.iutils.common.ResultVo;
import cn.iutils.common.controller.BaseController;
import cn.iutils.common.utils.JStringUtils;
import cn.iutils.faq.entity.Answers;
import cn.iutils.faq.service.AnswersService;
import cn.iutils.faq.service.QuestionsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
* 回答表 控制器
* @author iutils.cn
* @version 1.0
*/
@Controller
@RequestMapping("${adminPath}/faq/answers")
public class AnswersController extends BaseController {

    @Autowired
    private AnswersService answersService;

    @Autowired
    private QuestionsService questionsService;

    @ModelAttribute
    public Answers get(@RequestParam(required = false) String id) {
        Answers entity = null;
        if (JStringUtils.isNotBlank(id)) {
            entity = answersService.get(id);
        }
        if (entity == null) {
            entity = new Answers();
        }
        return entity;
    }

    /**
     * 我要回答
     * @param answers
     * @param model
     * @return
     */
    @RequiresPermissions("faq:answers:create")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create(Answers answers,Model model) {
        model.addAttribute("questions", questionsService.get(answers.getQuestionId()));
        model.addAttribute("answers", answers);
        return "faq/answers/form";
    }

    /**
     * 回答
     * @param answers
     * @param redirectAttributes
     * @return
     */
    @RequiresPermissions("faq:answers:create")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @Transactional
    public String create(Answers answers, RedirectAttributes redirectAttributes) {
        answersService.save(answers);
        addMessage(redirectAttributes, "回答成功");
        return "redirect:"+ adminPath +"/faq/questions/create?id="+answers.getQuestionId();
    }

    /**
     * 删除回答
     * @param questionId
     * @param id
     * @param redirectAttributes
     * @return
     */
    @RequiresPermissions("faq:answers:delete")
    @RequestMapping(value = "/{questionId}/{id}/delete", method = RequestMethod.GET)
    @Transactional
    public String delete(@PathVariable("questionId") String questionId,@PathVariable("id") String id, RedirectAttributes redirectAttributes) {
        answersService.delete(id);
        addMessage(redirectAttributes,"删除成功");
        return "redirect:"+ adminPath +"/faq/questions/view?id="+questionId;
    }

    /**
     * 回答问题
     * @return
     */
    @RequestMapping(value = "/submit", method = RequestMethod.POST)
    public @ResponseBody
    ResultVo submit(Answers answers){
        ResultVo resultVo = null;
        try{
            answersService.save(answers);
            resultVo = new ResultVo(ResultVo.SUCCESS,"1","调用成功",null);
        }catch (Exception e){
            logger.error("回答接口调用失败",e.getMessage());
            resultVo = new ResultVo(ResultVo.FAILURE,"-1","调用失败",null);
        }
        return resultVo;
    }

}
